const app = getApp();
var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
var qqmapsdk = new QQMapWX({
  key: 'RJ3BZ-UVWCV-WJSPH-UMZHG-QRZOZ-RMBQB'//申请的开发者秘钥key
});
var longitude
var latitude

Page({
  data: {
    navbar: ['医院', '药房'],
    currentTab: 0,
    mapId: ["map_hospital","map_pharmacy"],
    markers:[],
    longitude: 104.0017,  //默认定位经度
    latitude: 30.55695,
    mapkey:'TJTBZ-K7NLJ-GWHFD-KDDXZ-SOISV-J6BDN',
  },

  navbarTap: function(e){
    this.setData({
      currentTab: e.currentTarget.dataset.idx
    })
    this.onLoad()
    console.log('重新onload')
  },

  onLoad:function(){
    console.log('我现在在onload里')
    var that=this;
    wx.getLocation({
      type: "wgs84",
      success: function(res){
        latitude = res.latitude;
        longitude = res.longitude;
        //console.log(res.latitude);
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
          markers:[{
            id:-1,
            latitude: res.latitude,
            longitude: res.longitude
            
         }]
        })
      }
    })
    console.log(that.data.navbar[that.data.currentTab])
    qqmapsdk.search({
      keyword:that.data.navbar[that.data.currentTab],
      success: function (res) {    
        console.log(res);
        var hospital = [];
        for (var i = 0; i < res.data.length; i++) {
          hospital.push({
            id:i,
            longitude: res.data[i].location.lng,
            latitude: res.data[i].location.lat,
            iconPath: "/images/marker.png",
            title: res.data[i].title,
            address:res.data[i].address,
            distance:res.data[i]._distance,
            callout:{
              bgColor:'rgba(255, 255, 255, 0)',
            },
           
          })
        }
        that.setData({
          resultArray: res.data,
          markers: hospital
        })
      }
    })
  },

  //获取用户当前位置
  moveToLocation: function () {
    var that =this
    //mapId 就是你在 map 标签中定义的 id
    let Id = that.data.mapId[that.data.currentTab]
    var mapCtx = wx.createMapContext(Id);
    mapCtx.moveToLocation();
  },

  //跳转到外部地图
  goToHere:function(event){
    var id=event.markerId;
    var address=this.data.address;
    var latitute=this.data.latitude;
    var longtitude=this.data.longitude;
    var title=this.data.title;
    this.lunchMap(latitude,longitude,address,title);
  },

  lunchMap:function(latitude,longtitude,address,title){
    wx.openLocation({
      name:title,
      address:address,
      latitude: latitude,
      longitude: longitude,
    })
  },

  //显示对话框
  showModal: function(event) {
    console.log(event.markerId);
    var that = this;
    var id = event.markerId;
    var title=that.data.markers[id].title;
    var address=that.data.markers[id].address;
    var distance=that.data.markers[id].distance;
    that.setData({
      title:title,
      address:address,
      distance:distance,
    });
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function() {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  
  //隐藏对话框
  hideModal: function() {
    setTimeout(function() {
      console.log('我是hidemodal')
    }, 3000);
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function() {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },


})